export default (list = [], action) => {
  const { payload } = action
  list = [...list]
  switch (action.type) {
    case "musicList/add":
      if (payload.info) {
        if (list.length > 50) {
          list.shift()
        }
        list.forEach((item, index) => {
          if(item.id === payload.info.id){
            list.splice(index, 1)
          }
        })
        list.push(payload.info)
      }
      break;
    case "musicList/minus":
      if (payload.index || payload.index == 0) {
        list.splice(payload.index, 1)
      }
      break;
    case "musicList/addNextMusic":
    if ((payload.index || payload.index == 0) && (payload.oldIndex || payload.oldIndex == 0)) {
      let index = payload.index;
      let oldIndex = payload.oldIndex;
      if(oldIndex > index){
        oldIndex++
      }
      list.splice(index +  1, 0,list[payload.oldIndex])
      list.splice(oldIndex, 1)
    } else if((payload.index || payload.index == 0) && payload.info) {
      list.splice(index +  1, 0, payload.info)
    }
    break;
    case "musicList/empty":
        // list = []
        list.splice(0)
      break;
    default:
      break;
  }
  localStorage.musicList = list;
  return list;
}